package cn.bytecollege.service;

import cn.bytecollege.mapper.GoodsMapper;
import cn.bytecollege.mapper.OrderDetailMapper;
import cn.bytecollege.mapper.OrderMapper;
import cn.bytecollege.mapper.UserAddressMapper;
import cn.bytecollege.model.OrderDetail;
import cn.bytecollege.model.Orders;
import cn.bytecollege.model.UserAddress;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.bytecollege.mapper.UserMapper;
import cn.bytecollege.model.User;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 用户获取个人订单信息方法
     *
     * @param userId
     * @return
     */
    public List<Orders> getOrders(Integer userId) {
        //获取orders表中所有数据并放入orderList集合中
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("user_id", userId).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        List<Orders> orderList = orderMapper.selectList(wrapper);
        //遍历orderList集合
        for (Orders orders : orderList) {
            //在orderList中获取userAddressId
            Integer userAddressId = orders.getUserAddressId();
            //根据userAddressId在user_address表中查找数据并放入orderList对象的userAddress中
            orders.setUserAddress(userAddressMapper.selectById(userAddressId));
            //在orderList中获取orderId
            Integer orderId = orders.getOrderId();
            //根据orderId在order_detial表中查找数据并放入orderList对象的orderDetailList集合中
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id", orderId).select("order_detail_id", "order_id", "goods_id", "goods_price", "goods_number");
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(detailWrapper);
            orders.setOrderDetailList(orderDetailList);
            Double sum =0d;
            //遍历orderDetailList
            for (OrderDetail detail : orderDetailList) {
                //获取goodsId
                Integer goodsId = detail.getGoodsId();
                //根据goodsId在goods表中查找数据并放入orderDetail的goods对象中
                detail.setGoods(goodsMapper.selectById(goodsId));
                detail.setSum(detail.getGoodsPrice()*detail.getGoodsNumber());
                sum=sum+detail.getSum();
            }
            orders.setSum(sum);
        }
        return orderList;
    }

    public List<Orders> getOrdersBy(Integer userId,String orderState) {
        //获取orders表中所有数据并放入orderList集合中
        Map<String,String> map = new HashMap<>();
        map.put("user_id", String.valueOf(userId));
        map.put("order_state",orderState);
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().allEq(map).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        List<Orders> orderList = orderMapper.selectList(wrapper);
        //遍历orderList集合
        for (Orders orders : orderList) {
            //在orderList中获取userAddressId
            Integer userAddressId = orders.getUserAddressId();
            //根据userAddressId在user_address表中查找数据并放入orderList对象的userAddress中
            orders.setUserAddress(userAddressMapper.selectById(userAddressId));
            //在orderList中获取orderId
            Integer orderId = orders.getOrderId();
            //根据orderId在order_detial表中查找数据并放入orderList对象的orderDetailList集合中
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id", orderId).select("order_detail_id", "order_id", "goods_id", "goods_price", "goods_number");
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(detailWrapper);
            orders.setOrderDetailList(orderDetailList);
            Double sum =0d;
            //遍历orderDetailList
            for (OrderDetail detail : orderDetailList) {
                //获取goodsId
                Integer goodsId = detail.getGoodsId();
                //根据goodsId在goods表中查找数据并放入orderDetail的goods对象中
                detail.setGoods(goodsMapper.selectById(goodsId));
                detail.setSum(detail.getGoodsPrice()*detail.getGoodsNumber());
                sum=sum+detail.getSum();
            }
            orders.setSum(sum);
        }
        return orderList;
    }

    /**
     * 用户删除订单的方法
     *
     * @param orderId
     * @param
     * @return
     */
//    public int delete(Integer orderId) {
    public int delete(Integer orderId){
        //获取订单信息
        QueryWrapper<Orders> qWrapper = new QueryWrapper<Orders>().eq("order_id", orderId).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        Orders orders = orderMapper.selectOne(qWrapper);
        //判断订单状态 已取消，待评价，已评价状态可以进行删除订单
        if (orders.getOrderState().equals("已取消") || orders.getOrderState().equals("待评价") || orders.getOrderState().equals("已评价")) {
            //根据orderId删除orderDetail表中的数据
            UpdateWrapper<OrderDetail> wrapperDetail = new UpdateWrapper<OrderDetail>().eq("order_id", orderId);
            orderDetailMapper.delete(wrapperDetail);
            //根据orderId删除orders表中的数据
            UpdateWrapper<Orders> wrapper = new UpdateWrapper<Orders>().eq("order_id", orderId);
            orderMapper.delete(wrapper);
            return 1;
        }
        return 0;
    }

    /**
     * 用户修改确认收货方法
     *
     * @param orderId
     * @return
     */
    public int mod(Integer orderId) {
        //获取订单信息
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("order_id", orderId).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        Orders orders = orderMapper.selectOne(wrapper);
        //修改订单状态
        if(orders.getOrderState().equals("已取消")||orders.getOrderState().equals("待评价")||orders.getOrderState().equals("完成评价")||orders.getOrderState().equals("待支付")){
            return 0;
        }
        orders.setOrderState("待评价");
        return orderMapper.updateById(orders);
    }

    /**
     * 用户取消订单的方法
     *
     * @param orderId
     * @return
     */
    public int cancel(Integer orderId) {
        //获取订单信息
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("order_id", orderId).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        Orders orders = orderMapper.selectOne(wrapper);
        //处于待支付的订单可以取消
        if (orders.getOrderState().equals("待支付")) {
            orders.setOrderState("已取消");
            orderMapper.updateById(orders);
            return 1;
            //处于待揽件，待发货状态的订单需后台服务中心审核
        } else if (orders.getOrderState().equals("待揽件") || orders.getOrderState().equals("待发货")) {
            orders.setOrderCheck("待审核");
            orderMapper.updateById(orders);
            return 2;
            //处于待签收状态的订单无法取消
        } else if (orders.getOrderState().equals("待签收")) {
            orders.setOrderCheck("已拒绝");
            orderMapper.updateById(orders);
            return 0;
        }
        return 0;
    }

    public int addUser(User user) {
        return userMapper.insert(user);
    }

    public User getUser(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("username", username).select("user_id", "username", "password", "power");
        User users =  userMapper.selectOne(wrapper);
        return users;
    }

    public int modUser(User user) {
        return userMapper.updateById(user);
    }

    /**
     * 用户查看地址功能的方法
     * @param userId
     * @return
     */
    public List<UserAddress> getAddresses(Integer userId){
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<UserAddress>().eq("user_id",userId);
        return userAddressMapper.selectList(wrapper);
    }

    /**
     * 用户删除订单的方法
     * @param
     * @param userAddressId
     * @return
     */
    public int deleteAddress(Integer userAddressId){
        Map<String,Integer> map = new HashMap<>();
        map.put("user_address_id",userAddressId);
        UpdateWrapper<UserAddress> wrapper1 = new UpdateWrapper<UserAddress>().allEq(map);
        return userAddressMapper.delete(wrapper1);
    }

    /**
     * 添加用户地址方法
     * @param userAddress
     * @return
     */
    public int addAddress(UserAddress userAddress){
        return userAddressMapper.addAddress(userAddress);
    }

    public UserAddress modAddress(Integer userAddressId){
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<UserAddress>().eq("user_address_id",userAddressId);
        return userAddressMapper.selectOne(wrapper);
    }

    public int modAddress(UserAddress userAddress){
//        Map<String,Integer> map = new HashMap<>();
//        map.put("user_id",userAddress.getUserId());
//        map.put("user_address_id",userAddress.getUserAddressId());
//        UpdateWrapper<UserAddress> wrapper = new UpdateWrapper<UserAddress>().allEq(map);
       return userAddressMapper.modById(userAddress);
    }
    public List<User> getByUsername() {
        return userMapper.selectList(null);
    }

    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

}
